package com.mvc.controller.healthmanager;

import java.net.URLDecoder;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.huangqj.consant.ValidatorUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSON;
import com.mvc.controller.base.BaseController;
import com.mvc.framework.constant.GlobalConstant;
import com.mvc.model.base.Json;
import com.mvc.model.base.SessionInfo;
import com.mvc.model.healthmanager.archive.BloodPressure;
import com.mvc.model.healthmanager.archive.BloodSugar;
import com.mvc.service.healthmanager.BloodSugarServiceI;
@Controller
@RequestMapping("/sugar")
public class bloodSugarController  extends BaseController  {
	@Autowired
	private BloodSugarServiceI sugarService;
	/**
	 * 跳转健康数据血糖模块
	 * @param request
	 * @return
	 */
	@RequestMapping("/index")
	public String index(HttpServletRequest request){
		return "/bloodSugar";
	}
	/**
	 * 查询个人历次血糖信息
	 * @param session
	 * @return 
	 */
	@RequestMapping("/getbloodsugar_old")
	@ResponseBody
	public List<BloodSugar> getpressuremanager(BloodSugar bSugar, HttpSession session) {
		SessionInfo sessionInfo = (SessionInfo) session.getAttribute(GlobalConstant.SESSION_INFO);
		if(sessionInfo != null){
			bSugar.setPersonId(sessionInfo.getPersonId());
		}
		return sugarService.getBloodSugar(bSugar);
	}	
	
	/**
	 * 保存或更新血糖信息
	 * @param bs
	 * @return
	 */
	@RequestMapping("/saveOrUpdate")
	@ResponseBody
	public Json saveOrUpdate(HttpServletRequest request) throws Exception{
		String json = request.getParameter("json");
		System.out.println("未转化数据：" + json);
//		json = URLDecoder.decode(json, "utf-8");
		String[] s = json.split("=");
		if (s.length > 1) {
			json = s[1];
		}
		System.out.println("已转化数据：" + json);
		BloodSugar bs = new BloodSugar();
		if (!json.isEmpty()) {
			bs = JSON.parseObject(json, BloodSugar.class);
			bs.setId(null);
		}
		BloodSugar bSugar = sugarService.saveOrUpdate(bs);
		Json j = new Json();
		if(bSugar!=null&&!json.isEmpty()){
			j.setMsg("保存成功");
			j.setSuccess(true);
			j.setObj(bSugar);
		}else{
			j.setMsg("保存失败");
			j.setSuccess(false);
		}
		return j;
	}
	@RequestMapping(value="/saveOrUpdate", method=RequestMethod.POST,consumes = "application/json")
	@ResponseBody
	public Json saveOrUpdate(@RequestBody List<BloodSugar> bss,HttpServletRequest request) throws Exception{
		System.out.println("未转化的数据："+JSON.toJSONString(bss));
		List<BloodSugar> list = new ArrayList<BloodSugar>();
		Json j = new Json();
		if(bss.size()>0){
			for(BloodSugar bs : bss){
				Long appLocalId = ValidatorUtil.isNullOnLong(bs.getId())?null:bs.getId();
				bs.setId(null);
				BloodSugar bSugar = new BloodSugar();
				bSugar = sugarService.saveOrUpdate(bs);
				if(bSugar!=null&&!ValidatorUtil.isNullOnLong(bSugar.getId())){
					bSugar.setRespCode(0);
				}else{
					bSugar.setRespCode(-1);
				}
				bSugar.setAppLocalId(appLocalId);
				list.add(bSugar);
			}
			j.setMsg("保存成功");
			j.setSuccess(true);
			j.setObj(list);
		}else{
			j.setMsg("数据为空！");
			j.setSuccess(false);
		}
		return j;
	}
	
	@RequestMapping("/getNewBloodSugar")
	@ResponseBody
	public Json getNewBloodSugar(BloodSugar bs){
		Json j = new Json();
		BloodSugar bsSugar = sugarService.getNewBloodSugar(bs);
		if(!ValidatorUtil.isNullOnLong(bsSugar.getId())){
			j.setMsg("获取成功");
			j.setSuccess(true);
			j.setObj(bsSugar);
		}else{
			j.setMsg("获取数据为空");
			j.setSuccess(false);
		}
		return j;
	}
}
